<?php

namespace app\api\controller\carbon\calculation;

use app\common\controller\Api;
use think\Db;

/**
 * 核算记录接口
 */
class Log extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 按计算项获取核算记录
     *
     */
    public function infoCalculationLog()
    {
        $type = $this->request->request('type');
        $dt = $this->request->request('dt');
        $time_start = $this->request->request('time_start');
        $time_end = $this->request->request('time_end');
        $level = $this->request->request('level');
        $calculation_type_id = $this->request->request('calculation_type_id');
        $user_id = $this->auth->id;
        $user_id = 8;
        $LogModel = new \app\admin\model\carbon\calculation\Log;
        if ($type == 1) {
            if ($dt == 0) {
                $calculation_id = $this->request->request('calculation_id');
                $log = $LogModel
                    ->where(['calculation_id' => $calculation_id, 'user_id' => $user_id])
                    ->where($LogModel->getTable() . '.create_time', '>', $time_start)
                    ->where($LogModel->getTable() . '.create_time', '<', $time_end)
                    ->paginate();
                $this->success('获取成功', $log);
            } else {
                if ($level == 1) {
                    $where = array(
                        "type_superior_id" => $calculation_type_id
                    );
                } else {
                    $where = array(
                        "type_id" => $calculation_type_id
                    );
                }

                $log = model('app\admin\model\carbon\calculation\Log')
                    // ->with('calculation')
                    ->where(['user_id' => $user_id])
                    ->where($where)
                    ->where($LogModel->getTable() . '.create_time', '>', $time_start)
                    ->where($LogModel->getTable() . '.create_time', '<', $time_end)
                    ->field('calculation_id, SUM(result) as total_result')
                    ->group('calculation_id')
                    ->select();
                if ($dt == 1) {
                    $xAxis = [];
                    $series = [];
                    foreach ($log as $key => $value) {
                        $xAxis[] = $value['calculation']['name'];
                        $series[] = $value['total_result'];
                    }
                    $this->success('获取成功', ["xAxis" => $xAxis, "series" => $series]);
                } else if ($dt == 2) {
                    $data = [];
                    foreach ($log as $key => $value) {
                        $data[] = [
                            "value" => $value['total_result'],
                            "name" => $value['calculation']['name'],
                        ];
                    }
                    $this->success('获取成功', $data);
                }
            }
        } else if ($type == 2) {



            if ($dt == 0) {

                if ($level == 1) {
                    $where = array(
                        "type_superior_id" => $calculation_type_id
                    );
                } else {
                    $where = array(
                        "type_id" => $calculation_type_id
                    );
                }
                $log = model('app\admin\model\carbon\calculation\Log')
                    ->where($where)
                    ->where($LogModel->getTable() . '.create_time', '>', $time_start)
                    ->where($LogModel->getTable() . '.create_time', '<', $time_end)
                    ->paginate()
                    ->each(function ($log, $key) {
                        $log->calculation = model('app\admin\model\carbon\calculation\Info')
                            ->where(['id' => $log->calculation_id])
                            ->find();
                        return $log;
                    });
                $this->success('获取成功', $log);
            } else {
                if ($level == 1) {
                    $log = model('app\admin\model\carbon\calculation\Log')
                        ->with('typesuperior')
                        ->where(['user_id' => $user_id])
                        ->where($LogModel->getTable() . '.create_time', '>', $time_start)
                        ->where($LogModel->getTable() . '.create_time', '<', $time_end)
                        ->field('type_superior_id, SUM(result) as total_result')
                        ->group('type_superior_id')
                        ->select();
                    if ($dt == 1) {
                        $xAxis = [];
                        $series = [];
                        foreach ($log as $key => $value) {

                            $xAxis[] = $value['type_superior']['name'];
                            $series[] = $value['total_result'];
                        }
                        $this->success('获取成功', ["xAxis" => $xAxis, "series" => $series]);
                    } else if ($dt == 2) {
                        $data = [];
                        foreach ($log as $key => $value) {
                            $data[] = [
                                "value" => $value['total_result'],
                                "name" => $value['type_superior']['name'],
                            ];
                        }
                        $this->success('获取成功', $data);
                    }
                } elseif ($level == 2) {

                    $log = model('app\admin\model\carbon\calculation\Log')
                        // ->with('type')
                        ->where(['user_id' => $user_id])
                        ->where(['type_superior_id' => $calculation_type_id])
                        ->where($LogModel->getTable() . '.create_time', '>', $time_start)
                        ->where($LogModel->getTable() . '.create_time', '<', $time_end)
                        ->field('type_id, SUM(result) as total_result')
                        ->group('type_id')
                        ->select();

                    if ($dt == 1) {
                        $xAxis = [];
                        $series = [];
                        foreach ($log as $key => $value) {
                            // print_r((array)$value);
                            $xAxis[] = $value['type']['name'];
                            // unset($value['type']);
                            $series[] = $value['total_result'];
                        }
                        // $this->success('获取成功', $logss);
                        $this->success('获取成功', ["xAxis" => $xAxis, "series" => $series]);
                    } else if ($dt == 2) {
                        $data = [];
                        foreach ($log as $key => $value) {
                            $data[] = [
                                "value" => $value['total_result'],
                                "name" => $value['type']['name'],
                            ];
                        }
                        $this->success('获取成功', $data);
                    }
                }
            }
        }
    }







    /**
     * 按计算类型获取核算记录
     *
     */
    public function typeCalculationLog()
    {

        $time_start = $this->request->request('time_start');
        $time_end = $this->request->request('time_end');
        $type = $this->request->request('type');
        if ($this->auth->id) {
            $user = $this->auth->getUserinfo();
            // $this->success('获取成功',  $user);
            if ($type == 0) {
                $result = model('app\admin\model\carbon\calculation\Log')
                    ->where('school_id', $user["school_id"])
                    ->where('create_time', '>', $time_start)
                    ->where('create_time', '<', $time_end)
                    ->sum("result");
            } else {
                $result = model('app\admin\model\carbon\calculation\Log')
                    ->where('user_id', $user["id"])
                    ->where('create_time', '>', $time_start)
                    ->where('create_time', '<', $time_end)
                    ->sum("result");
            }
            $this->success('获取成功',  $result);
        } else {
            $this->success('获取成功',  0);
        }
    }



    /**
     * 按省份城市获取核算记录(排名)
     *
     */
    public function araeCalculationLog()
    {
        $typ = $this->request->request('type');
        $time_start = $this->request->request('time_start');
        $time_end = $this->request->request('time_end');
        $LogModel = new \app\admin\model\carbon\calculation\Log;
        if ($typ == 'province') {
            $list = $LogModel
                ->field('province_id, SUM(result) as total_result')
                ->where($LogModel->getTable() . '.create_time', '>', $time_start)
                ->where($LogModel->getTable() . '.create_time', '<', $time_end)
                ->group('province_id')
                ->select();
            foreach ($list as $key => $value) {
                $value->arae = model('app\admin\model\Area')
                    ->where(['id' => $value->province_id])
                    ->find();
            }
        } elseif ($typ == 'city') {
            $list = $LogModel
                ->field('city_id, SUM(result) as total_result')
                ->where($LogModel->getTable() . '.create_time', '>', $time_start)
                ->where($LogModel->getTable() . '.create_time', '<', $time_end)
                ->group('city_id')
                ->select();
            foreach ($list as $key => $value) {
                $value->arae = model('app\admin\model\Area')
                    ->where(['id' => $value->city_id])
                    ->find();
            }
        }
        $this->success('获取成功', $list);
    }



    /**
     * 按学校获取核算记录(排名)
     *
     */
    public function schoolCalculationLog()
    {
        $list = model('app\admin\model\carbon\calculation\Log')
            ->field('school_id, SUM(result) as total_result')
            ->group('school_id')
            ->paginate()
            ->each(function ($log, $key) {
                $log->school = model('app\admin\model\carbon\campus\School')
                    ->where(['id' => $log->school_id])
                    ->find();
                return $log;
            });
        $this->success('获取成功', $list);
    }

    /**
     * 按耗能类型取核算记录(排名)
     *
     */
    public function energyCalculationLog()
    {
        $list = model('app\admin\model\carbon\calculation\Type')
            ->where('level', 1)
            ->select();
        foreach ($list as $key => $value) {
            $value->juniorlist = model('app\admin\model\carbon\calculation\Type')
                ->where('superior_id', $value['id'])
                ->where('level', 2)
                ->select();
        }
        $this->success('获取成功', $list);
    }

    /**
     * 按时间取核算记录(排名)
     *
     */
    public function timeCalculationLog()
    {
        $list = model('app\admin\model\carbon\calculation\Type')
            ->where('level', 1)
            ->select();
        foreach ($list as $key => $value) {
            $value->juniorlist = model('app\admin\model\carbon\calculation\Type')
                ->where('superior_id', $value['id'])
                ->where('level', 2)
                ->select();
        }
        $this->success('获取成功', $list);
    }
}
